package com.cskaoyan.servlet;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.CouponListUserParameter;
import com.cskaoyan.bean.CouponParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketCoupon;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.impl.CoupouServiceImpl;
import com.cskaoyan.utils.JacksonUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/admin/coupon/*")
public class AdminCouponServlet extends CommonServlet {

    CouponService couponService = new CoupouServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        CouponParameter parameter = new CouponParameter();
        Map<String, String[]> parameterMap = request.getParameterMap();
        BeanUtils.copyProperties(parameter,parameterMap);

       PageData data = couponService.querySelective(parameter);

        return BaseRespVo.ok(data);
    }

    public BaseRespVo create(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(jsonStr, MarketCoupon.class);
        couponService.create(marketCoupon);

        return BaseRespVo.ok(marketCoupon);
    }
    public BaseRespVo update(HttpServletRequest request,HttpServletResponse response) throws IOException {

        String jsonStr = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(jsonStr, MarketCoupon.class);

        couponService.updateCoupon(marketCoupon);

        return BaseRespVo.ok(null);
    }

    public BaseRespVo delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.read(jsonStr, MarketCoupon.class);

        couponService.deleteCoupon(marketCoupon);

        return BaseRespVo.ok(null);
    }

    public BaseRespVo listuser(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        Map<String, String[]> parameterMap = request.getParameterMap();
        CouponListUserParameter parameter = new CouponListUserParameter();

        BeanUtils.copyProperties(parameter,parameterMap);
        PageData data= couponService.listuser(parameter);

        return BaseRespVo.ok(data);
    }
}
